import wepy from 'wepy'
export default class extends wepy.mixin {
	data = {
		// 订单id
		OrderId: '',
		// 订单详情数据
		orderDatilList: {},
		// 预支付订单信息
		yuOrderInfo: {},
		showing: false,
		jifen:'',
		hunhezhifu:false,
		jiaoyimima:''
	}
	onLoad(options) {
		console.log(options)
		this.OrderId = options.id
		this.getOrderDatil()
		console.log('交易状态', this.status)
	}
	onShow() {
		this.jifenlist()
	}
	methods = {
		// 跳转到商品详情
		goGoodsDatial(id) {
			console.log('跳转到商品详情', id)
			wepy.navigateTo({
				url: '../../pages/goodsDatils/gooddatils?id=' + id,
			})
		},
		// 点击显示和隐藏
		hanledianji() {
			console.log('点击了隐藏')
			this.showing = !this.showing
		},
		// 去支付
		async onSubmit() {
			const res = await wepy.getNetworkType({ //检查网络状态

			})
			console.log(res)
			if (res.networkType == 'none') {
				wepy.noToast('请检查网络设置后重试')
			} else {
				if (this.orderDatilList.payType == 3) {
					console.log("选择了全积分支付")
					this.hunhezhifu = true;
					this.$apply()
				}else if (this.orderDatilList.payType == 4) {
					console.log("选择了积分+现金支付")
					this.zhifus()
				}else if (this.orderDatilList.payType == 0 ) {
					console.log("普通商品人民币支付")
					this.zhifus()
				}else if (this.orderDatilList.payType == 1) { //单商品
					if(this.orderDatilList.totalPrice==0){ //单商品全R点支付
						this.hunhezhifu = true;
						this.$apply()
					}else {  //R点+现金支付
						this.zhifus()
					}
				}
			}
		},
		bindKeyInput(event){
			console.log(event)
			this.jiaoyimima=event.detail.value
		},
		//全积分或全R点支付
		async jifendian() {
			if(this.jiaoyimima != ''){
				const res = await wepy.post('baseRest/checkTradePwd', {
				  memberId:wepy.getStorageSync('userId'),
				  opwd: this.jiaoyimima,
				})
				console.log(res)
				if(res.data.state==1){
					this.hunhezhifu=false;
					this.$apply()
					this.zhifus()
				}else{
					wepy.noToast(res.data.message);
				}
			}else{
				wepy.noToast('请输入交易密码');
			}
		}
	}
	computed = {
		empty() {
			if (this.orderDatilList.qhCode) {
				return true
			}
			return false
		}
	}
	//支付
	async zhifus() {
		const {
			data: result
		} = await wepy.post('baseRest/toWechatpay', {
			orderId: this.OrderId,
		})
		console.log(result)
		if (result.state != 1) {
			return wepy.noToast(result.message)
		}
		this.yuOrderInfo = result.data
		this.$apply()
		// 调用微信支付接口
		const payResult = await wx
			.requestPayment({
				timeStamp: this.yuOrderInfo.timestamp,
				nonceStr: this.yuOrderInfo.nonceStr,
				package: this.yuOrderInfo.package,
				signType: this.yuOrderInfo.signType,
				paySign: this.yuOrderInfo.paySign,
			})
			.catch((err) => err)
		// 用户取消了支付
		if (payResult.errMsg == 'requestPayment:fail cancel') {
			wepy.redirectTo({
				url: '../../pages/Order/order?i=' + 0,
			})
			return wepy.noToast('您已取消了支付！')
		}else{
			wepy.redirectTo({
				url: '../../pages/Order/order?i=' + 0,
			})
		}
	}
	// 获取订单详情
	async getOrderDatil() {
		const {
			data: res
		} = await wepy.post('baseRest/getOrderDetail', {
			id: this.OrderId,
		})
		console.log(res)
		if (res.state != 1) {
			return wepy.noToast('网络异常，请稍后尝试')
		}
		this.orderDatilList = res.data
		this.$apply()
	}
	// 获取积分
	async jifenlist() {
		const res = await wepy.post('baseRest/getMemberRCoinAndJf', {
			memberId: wepy.getStorageSync('userId'),
		})
		console.log(res,"积分")
		if (res.statusCode != 200) {
			return wepy.noToast('获取数据失败')
		}
		this.jifen = res.data.data
		this.$apply()
	}
}
